﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Categorys;
using MpSoft.Library.DAO.Contents;
using MpSoft.Library.DAO.Customers;
using MpSoft.Library.DAO.Products;
using MpSoft.Library.DAO.Settings;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;

namespace MpSoft.Library.BUSS.Settings
{

    #region  Setting
    public class SettingBUSS
    {
        private const string SETTING_ALL_KEY = "Mp.setting.all";
        private const string SETTING_BY_ID_KEY = "Mp.mp_setting.key-{0}";

        private static void RemoveCacheSetting()
        {
            MpCache.RemoveByPattern(SETTING_ALL_KEY);
            MpCache.RemoveByPattern(SETTING_BY_ID_KEY);
        }

        public static int Setting_Add(string SettingKey, string SettingValue)
        {
            var result = Setting_Add(SettingKey, 1, SettingValue, null, null);
            return result;
        }

        public static int Setting_Add(string SettingKey, int LanguageID, string SettingValue, string SettingGroup, int? SettingType)
        {
            SettingKey = SettingKey.ToLowerInvariant();
            var result = SettingDAO.Setting_Add(SettingKey, LanguageID, SettingValue, SettingGroup, SettingType);
            if (result > 0)
                RemoveCacheSetting();
            return result;
        }
        

        public static int Setting_Update(Setting obj)
        {
            var result = SettingDAO.Setting_Update(obj);
            if (result > 0)
                RemoveCacheSetting();
            return result;
        }

        public static int Setting_Delete(int SettingID, string SettingKey)
        {
            var result = SettingDAO.Setting_Delete(SettingID, SettingKey);
            if (result > 0)
                RemoveCacheSetting();
            return result;
        }
        public static Setting Setting_ByID(int SettingID)
        {
            string key = string.Format(SETTING_BY_ID_KEY, SettingID);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (Setting)obj;
            var list = Setting_List(SettingID, null);
            Setting obj2 = null;
            if (list != null)
                obj2 = list.FirstOrDefault();
            MpCache.Max(key, obj2);
            return obj2;
        }

        public static string GetSettingValue(string SettingKey)
        {
            return GetSettingValue(SettingKey, null);
        }

        public static string GetSettingValue(string SettingKey, string Value)
        {
            var obj = Setting_ByKey(SettingKey);
            if (obj != null)
                return obj.SettingValue;
            Setting_Add(SettingKey, Value);
            return Value;
        }

        public static Setting Setting_ByKey(string SettingKey)
        {
            if (string.IsNullOrEmpty(SettingKey))
                return null;
            SettingKey = SettingKey.ToLowerInvariant();
            var settings = Setting_List();
            if (settings.ContainsKey(SettingKey))
            {
                var setting = settings[SettingKey];
                return setting;
            }
            return null;
        }

        public static Dictionary<string, Setting> Setting_List()
        {
            var key = SETTING_ALL_KEY;
            object obj = MpCache.Get(key);
            if (obj != null)
                return (Dictionary<string, Setting>)obj;
            var list = Setting_List(-1, string.Empty).OrderBy(item => item.SettingKey).ToDictionary(item => item.SettingKey.ToLowerInvariant());
            MpCache.Max(key, list);
            return list;

        }
        public static List<Setting> Setting_List(int SettingID, string SettingKey)
        {
            return SettingDAO.Setting_List(SettingID, SettingKey);
        }
    }
    #endregion

}
